package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exceptions.LeadnewsException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.SensitiveDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2021-03-31
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public PageResult<SensitiveDto> findByPage(SensitiveDto sensitiveDto) {
        //设置分页条件
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        //设置查询条件
        QueryWrapper<AdSensitive> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(sensitiveDto.getSensitives())){
            queryWrapper.lambda().like(AdSensitive::getSensitives,sensitiveDto.getSensitives());
        }
        //分页查询
        IPage<AdSensitive> SensitiveIPage = page(page, queryWrapper);
        //判断
        if (CollectionUtils.isEmpty(SensitiveIPage.getRecords())){
            throw new LeadnewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        List<AdSensitive> list = BeanHelper.copyWithCollection(SensitiveIPage.getRecords(), AdSensitive.class);
        return new PageResult(sensitiveDto.getPage(),
                                sensitiveDto.getSize(),
                                SensitiveIPage.getTotal(),
                                list);
    }

}
